/**
 * Object Script Start
 */

(function ( $ ) {
	$.fn.menuEffect = function(options) {
		var defaults = {
		};
		
		var opts = $.extend({}, defaults, options);
		
		var topMenu;
		var topMenuItem;
		
		var curMenuIdx;
		var curSubMenu;
		var menuAreaList = new Array();
		var menuArea;
		var areaTrack;
	
		topMenu = $(this);
		topMenuItem = topMenu.children("li");
		subMenu = topMenu.parents().children(".secondMenu");
		
		if( opts.displayTrack ){
			areaTrack = $("#areaCanvas").svCanvas(options);
		}
		
		subMenu.fadeOut( "fast" );
		
		var menuTimeoutObj;
		var subMenu;
		
		this.clear = function(){
			areaTrack.clear();
		};
		
		this.hideMenu = function (){
		    this.cancelHideMenu();
		    this.menuTimeoutObj = setTimeout("hideMenuWork()",700);
		};
		
		this.cancelHideMenu = function (){
		    if( this.menuTimeoutObj != null ){
		        clearTimeout( menuTimeoutObj );
		        this.menuTimeoutObj = null;
		    }
		};
		
		this.hideMenuWork = function (){
		    this.subMenu.fadeOut( 300 );
		    this.curMenuIdx = null;
		    this.menuArea = null;
		    this.menuAreaList = new Array();
		};

		eventTracker = new multiLineObj();
		
		topMenu.bind({
			mouseenter : function (){
				topMenu.cancelHideMenu();
			}
			, mouseleave : function (){
				topMenu.hideMenu();
			}
		});
		
		subMenu.bind({
			mouseenter : function (){
				topMenu.cancelHideMenu();
			}
			, mouseleave : function (){
				topMenu.hideMenu();
			}
		});
		
		topMenuItem.bind({
			mousemove : function(event){
			//click : function(event){
				
				var curObj = event.target;
				var eventIdx = curObj.getAttribute( 'itemIdx' );
				var curPos = null;
				var subPos = null;
				var eventPoint = new pointObj( event.pageX, event.pageY );
				
				eventTracker.addPoint(eventPoint);
				
				if( eventIdx < subMenu.length ){
					
					curObj = $(".firstMenu > li:eq(" + eventIdx + ")");
				
					curPos = curObj.position();
					
					var areaFlag = false;
					
					if(menuAreaList.length > 4){
						menuArea = menuAreaList.shift();
						areaFlag = menuArea.isInShape( eventPoint );
					}
					
					/**
					 * cursor event is not in the area and menu index has changed
					 */
					if( areaFlag != true && curMenuIdx != eventIdx ){
						
						if( curSubMenu != null ){
							curSubMenu.fadeOut( "fast" );
						}
						
						curMenuIdx = eventIdx;
						
						curSubMenu = $(".secondMenu:eq(" + eventIdx + ")");
	
						curSubMenu.css( "top", curPos.top );
						curSubMenu.css( "left", curPos.left + curObj.outerWidth() );
						
						curSubMenu.fadeIn( "fast" );
						
					}
					
					if(curSubMenu != null){
						subPos = curSubMenu.position();
						
						menuArea = new shapeObj();
						/**
						 * mouse point
						 */
						menuArea.addPoint( eventPoint );
						/**
						 * sub menu top left point
						 */
						menuArea.addPoint( new pointObj( subPos.left, subPos.top ) );
						/**
						 * sub menu bottom left point
						 */
						menuArea.addPoint( new pointObj( subPos.left, ( subPos.top + curSubMenu.outerHeight() ) ) );
	
						menuAreaList.push( menuArea );
						
					}
					
					if( opts.displayTrack ){
						areaTrack.addGeoObj( menuAreaList[ 0 ] );
						areaTrack.addGeoObj( eventTracker );
						areaTrack.drawAll();
					}
					
				}
			}
		}
		);
		
		return this.each(function(){$this = $(this);});
		
	};
})(jQuery);
